<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" href="${ctx}/css/common.css" />
<link rel="stylesheet" href="${ctx}/css/validate.css" />
<link rel="stylesheet" href="${ctx}/css/dialog/skin/bluebar/ymPrompt.css" />

<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/dialog/ymPrompt.js"></script>

<script language="javascript">
<c:if test="${action == 'SaveAdd'}">
ymPrompt.succeedInfo("<s:text name='AddEntitySuccess' />！",null,null,null,refresh);
</c:if>
<c:if test="${action == 'SaveClose'}">
ymPrompt.succeedInfo("<s:text name='AddEntitySuccess' />！",null,null,null,close);
</c:if>
<c:if test="${action == 'Update'}">
ymPrompt.succeedInfo("<s:text name='UpdateEntitySuccess' />！",null,null,null,close);
</c:if>

$(document).ready(function() {
	$("#app").focus();
});

//对话框回调方法
function close(tp){
	if(tp=="ok"){
		$("#mainFrame",window.parent.document).attr("src","entityManagerPage.action");
		parent.ymPrompt.close();
	}
}

//关闭对话框
function closeWin(){
	parent.ymPrompt.close();
}

//刷新页面
function refresh(tp){
	if(tp=="ok"){
		$("#mainFrame",window.parent.document).attr("src","entityManagerPage.action");
		window.location.href="editEntityPage.action";
	}
}

function formSubmit(action){
	if(checkForm()){
		if(action == 1) { //保存新增
			$("#action").val("SaveAdd");
		} else if(action == 2){ //保存关闭
			$("#action").val("SaveClose");
		}
		$("#appForm").submit();
	} else {
		return false;
	}
}

function checkForm(){
	var appFlag = true;
	var entityNameFlag = true;
	var entityTypeFlag = true;
	var entityCodeFlag = true;

	if($("#app").val() == "0"){
		$("#app").addClass("error");
		$("#appErrMsg").text("<s:text name='SelectApp' />");
		appFlag = false;
	} else {
		$("#app").removeClass("error");
		$("#appErrMsg").text("*");
		appFlag = true;
	}
	
	if($("#entityName").val().replace(/\s+/g,'') == ""){
		$("#entityName").addClass("error");
		$("#entityNameErrMsg").text("<s:text name='EntityNameIsNotNull' />");
		entityNameFlag = false;
	} else {
		$("#entityName").removeClass("error");
		$("#entityNameErrMsg").text("*");
		entityNameFlag = true;
	}
	
	if($("#entityType").val() == "0"){
		$("#entityType").addClass("error");
		$("#entityTypeErrMsg").text("<s:text name='SelectEntityType' />");
		entityTypeFlag = false;
	} else {
		$("#entityType").removeClass("error");
		$("#entityTypeErrMsg").text("*");
		entityTypeFlag = true;
	}
	
	if($("#entityCode").val().replace(/\s+/g,'') == ""){
		$("#entityCode").addClass("error");
		$("#entityCodeErrMsg").text("<s:text name='EntityCodeIsNotNull' />");
		entityCodeFlag = false;
	} else {
		$("#entityCode").removeClass("error");
		$("#entityCodeErrMsg").text("*");
		entityCodeFlag = true;
	}
	
	if(appFlag && entityNameFlag && entityTypeFlag && entityCodeFlag){
		return true;
	} else {
		return false;
	}
}

$(document).ready(function() {
	/**
	 * 应用选择操作
	 */
	$("#app").change(function(){
		var ajaxUrl = "../ajax/getEntitiesByApp.action?appid=" + $("#app").val();
		$.getJSON(ajaxUrl,function(data){
			$("#parent").empty();
			
			$("#parent").append("<option value=\"0\"><s:text name='SelectParentEntity' /></option>");
			
			$.each(data,function(id,value){
				if(value.parent == null) {
					//MENU
					$("#parent").append("<option value=\""+value.id+"\">|--"+value.entityName+"</option>");
					
					$.each(value.child,function(pageId,page){
						//PAGE
						$("#parent").append("<option value=\""+page.id+"\">&nbsp;&nbsp;|--"+page.entityName+"</option>");
						
						$.each(page.child,function(buttonId,button){
							//BUTTON
							$("#parent").append("<option value=\""+button.id+"\">&nbsp;&nbsp;&nbsp;&nbsp;|--"+button.entityName+"</option>");
						});
					});
				}
			});
		});
	});
});
</script>
</head>

<body>
<c:if test="${entity == null}">
<form id="appForm" name="appForm" action="addEntity.action" method="post" onsubmit="return $('#action').val() == 'SaveAdd'?formSubmit(1):formSubmit(2)">
</c:if>
<c:if test="${entity != null}">
<form id="appForm" name="appForm" action="updateEntity.action" method="post" onsubmit="return formSubmit(2)">
</c:if>
	<input id="id" name="id" type="hidden" value="${entity.id }" />
	<input id="action" name="action" type="hidden" value="${action }" />
	<table width="100%" cellspacing="1" cellpadding="5" class="table_class">
		<tr>
			<td width="20%"><s:text name='App' />：</td>
			<td>
				<select id="app" name="app">
					<c:if test="${entity != null }">
					<option value="0"><s:text name='SelectApp' /></option>
					<c:forEach var="app" items="${apps}">
						<c:if test="${entity.application.id == app.id }">
						<option value="${app.id }" selected="selected">${app.appName }</option>
						</c:if>
						<c:if test="${entity.application.id != app.id }">
						<option value="${app.id }">${app.appName }</option>
						</c:if>
					</c:forEach>
					</c:if>
					<c:if test="${entity == null }">
					<option value="0" selected="selected"><s:text name='SelectApp' /></option>
					<c:forEach var="app" items="${apps}">
						<option value="${app.id }">${app.appName }</option>
					</c:forEach>
					</c:if>
				</select>
				<span id="appErrMsg" class="inputMsg">*</span>
			</td>
		</tr>
		<tr>
			<td width="20%"><s:text name='EntityName' />：</td>
			<td>
				<input id="entityName" name="entityName" type="text" value="${entity.entityName }" />
				<span id="entityNameErrMsg" class="inputMsg">*</span>
			</td>
		</tr>
		<tr>
			<td width="20%"><s:text name='EntityType' />：</td>
			<td>
				<select id="entityType" name="entityType">
					<c:if test="${entity != null }">
					<option value="0"><s:text name='SelectEntityType' /></option>
					<c:forEach var="entityType" items="${entityTypes}">
						<c:if test="${entity.entityType.id == entityType.id }">
						<option value="${entityType.id }" selected="selected">${entityType.entityType }</option>
						</c:if>
						<c:if test="${entity.entityType.id != entityType.id }">
						<option value="${entityType.id }">${entityType.entityType }</option>
						</c:if>
					</c:forEach>
					</c:if>
					<c:if test="${entity == null }">
					<option value="0"><s:text name='SelectEntityType' /></option>
					<c:forEach var="entityType" items="${entityTypes}">
						<option value="${entityType.id }">${entityType.entityType }</option>
					</c:forEach>
					</c:if>
				</select>
				<span id="entityTypeErrMsg" class="inputMsg">*</span>
			</td>
		</tr>
		<tr>
			<td width="20%"><s:text name='EntityCode' />：</td>
			<td>
				<input id="entityCode" name="entityCode" type="text" value="${entity.entityCode }" />
				<span id="entityCodeErrMsg" class="inputMsg">*</span>
			</td>
		</tr>
		<tr>
			<td width="20%"><s:text name='ParentEntityType' />：</td>
			<td>
				<select id="parent" name="parent">
					<option value="0"><s:text name='SelectParentEntity' /></option>
					
					<c:if test="${entity != null }">
					<c:forEach var="e" items="${entitys}">
						<c:if test="${e.parent == null}">
						<c:if test="${e.id == entity.entitys.id }">
						<option value="${e.id }" selected="selected">|--${e.entityName }</option>
						</c:if>
						<c:if test="${e.id != entity.entitys.id }">
						<option value="${e.id }">|--${e.entityName }</option>
						</c:if>
						<c:forEach var="page" items="${e.child}">
						<c:if test="${page.id == entity.entitys.id }">
						<option value="${page.id }" selected="selected">&nbsp;&nbsp;|--${page.entityName }</option>
						</c:if>
						<c:if test="${page.id != entity.entitys.id }">
						<option value="${page.id }">&nbsp;&nbsp;|--${page.entityName }</option>
						</c:if>
						<c:forEach var="button" items="${page.child}">
						<c:if test="${button.id == entity.entitys.id }">
						<option value="${button.id }" selected="selected">&nbsp;&nbsp;&nbsp;&nbsp;|--${button.entityName }</option>
						</c:if>
						<c:if test="${button.id != entity.entitys.id }">
						<option value="${button.id }">&nbsp;&nbsp;&nbsp;&nbsp;|--${button.entityName }</option>
						</c:if>
						</c:forEach>
						</c:forEach>
						</c:if>
					</c:forEach>
					</c:if>
					<%--<c:if test="${entity == null }">
					<c:forEach var="e" items="${entitys}">
						<c:if test="${e.parent == null}">
						<option value="${e.id }">|--${e.entityName }</option>
						<c:forEach var="page" items="${e.child}">
						<option value="${page.id }">&nbsp;&nbsp;|--${page.entityName }</option>
						<c:forEach var="button" items="${page.child}">
						<option value="${button.id }">&nbsp;&nbsp;&nbsp;&nbsp;|--${button.entityName }</option>
						</c:forEach>
						</c:forEach>
						</c:if>
					</c:forEach>
					</c:if>
				--%></select>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<c:if test="${entity == null}">
				<input id="submitBtn" name="submitBtn" type="button" onclick="formSubmit(1);" value="<s:text name='SaveAndAdd' />" />
				<input id="submitBtn" name="submitBtn" type="button" onclick="formSubmit(2);" value="<s:text name='SaveAndClose' />" />
				<input id="submitBtn" name="submitBtn" type="button" onclick="closeWin();" value="<s:text name='Close' />" />
				</c:if>
				<c:if test="${entity != null}">
				<input id="submitBtn" name="submitBtn" type="button" onclick="formSubmit(2);" value="<s:text name='Save' />" />
				<input id="submitBtn" name="submitBtn" type="button" onclick="closeWin();" value="<s:text name='Close' />" />
				</c:if>
			</td>
		</tr>
	</table>
</form>
</body>

</html>
